<?php
class cmsEditAction extends sfActions
{
	public function execute($request)
	{
		$this->cms_id = $request->getParameter('cms_id');
		if ($this->cms_id) {
			$this->cms = Doctrine_Core::getTable('GlCms')->find($this->cms_id);
			if ($this->cms)
				$this->cms_form = new AdminCmsForm($this->cms);
		}
		if ($request->hasParameter('cms')) {
			$form = $request->getParameter('cms');
			
			$this->status = $this->updateCmsFromRequest($this->cms, $form);
			try {
				$files = $this->getRequest()->getFiles();
				$file = $files['cms']['picture'];
				if ($file['size'] > 0)
				{
					$tmpName = $file['tmp_name'];
					$fileName = $file['name'];
					$fileExt = trim(strrchr($fileName,'.'));
	
					$img = new sfImage($tmpName, $file['type']);
					$img->saveAs(sfConfig::get('sf_upload_dir').'/cms/'.$fileName, $file['type']);
					
					$img1 = new sfImage(sfConfig::get('sf_upload_dir').'/cms/'.$fileName, $file['type']);
					$fileName1 = ($this->cms->cms_id).$fileExt;
					$img1->thumbnail(170,235,'center');
					$img1->setQuality(100);
					$img1->saveAs(sfConfig::get('sf_upload_dir').'/cms/'.$fileName1, $file['type']);
					
					$img2 = new sfImage(sfConfig::get('sf_upload_dir').'/cms/'.$fileName, $file['type']);
					$fileName2 = "big_slide_".$fileName1;
					$img2->thumbnail(957,343,'center');
					$img2->setQuality(100);
					$img2->saveAs(sfConfig::get('sf_upload_dir').'/cms/'.$fileName2, $file['type']);
					
					$img3 = new sfImage(sfConfig::get('sf_upload_dir').'/cms/'.$fileName, $file['type']);
					$fileName2 = "slide_".$fileName1;
					$img3->thumbnail(154,234,'center');
					$img3->setQuality(100);
					$img3->saveAs(sfConfig::get('sf_upload_dir').'/cms/'.$fileName2, $file['type']);
					
					$this->cms->picture = $fileName1;
					$this->cms->save();
					$this->upload_success = true;		
				}
			} catch (sfException $e) {
				//echo $e->getMessage();
			}
			
			if ($this->status) {
				$this->cms->save();
				$this->redirect('cms/cmsList?cms_category_id='.$this->cms->cms_category_id);
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updateCmsFromRequest(&$cms, $form)
	{
		if ($form['content'])
			$cms->content = $form['content'];
		if ($form['name'])
			$cms->name = $form['name'];	
		if ($form['is_active'])
			$cms->is_active = 1;
		else
			$cms->is_active = 0;
		if ($form['description'])
			$cms->description = $form['description'];

		$cms->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>